package com.wave12.utility;

import com.wave12.pro_cart.pub.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SmsUtil {
    private static final Logger logger = LoggerFactory.getLogger(SmsUtil.class);

    private static String mUser = "GZJT000258";
    private static String mPassword = "ds@668";
    private static String mSmsServer = "https://sdk3.028lk.com:9988";   // 林凯短信
    private static String mSign = "【盛德检测】";

    public static int sendSms(String phone, String con){
        int ret = 0;
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String strDt = sdf.format(dt);
        con += mSign;
        String gbkCon = "";
        try {
            gbkCon = URLEncoder.encode(
                    con.replaceAll("<br/>", " "), "GBK");
        }
        catch (Exception e){
            logger.warn(ErrorCode.SMS_SEND_ERROR.desc());
            return ErrorCode.SMS_SEND_ERROR.code();
        }

        String param = String.format("CorpID=%s&Pwd=%s&Mobile=%s&Content=%s&Cell=1&SendTime=%s",
                mUser, mPassword, phone, gbkCon, strDt);
        String retData = HttpRequest.sendPost(mSmsServer + "/BatchSend2.aspx", param);
        ret = Integer.valueOf(retData);
        if (ret > 0){
            logger.info("发送短信到" + phone + "成功, 内容：" + gbkCon);
        }
        else{
            String errorInfo = getSendSmsError(ret);
            logger.warn("发送短信到" + phone + "错误, 原因: " + errorInfo + ", 短信内容：" + gbkCon);
        }
        return ret;
    }

    public static int getRestSmsNums(){
        int ret = 0;
        String param = String.format("CorpID=%s&Pwd=%s",
                mUser, mPassword);
        String retData = HttpRequest.sendPost(mSmsServer + "/SelSum.aspx", param);
        ret = Integer.valueOf(retData);
        if (ret > 0){
            logger.info("查询到短信余额：" + ret);
        }
        else{
            String errorInfo = getSendSmsError(ret);
            logger.warn("查询到短信余额错误, 原因: " + errorInfo);
        }
        return ret;
    }

    private static String getSendSmsError(int code){
        if (code == -1){
            return "账号未注册";
        }
        else if (code == -2){
            return "其他错误；网络超时（ping地址，复制URL到IE检查）；定时格式不正确；密码中有#";
        }
        else if (code == -3){
            return "帐号或密码错误";
        }
        else if (code == -5){
            return "余额不足，请充值";
        }
        else if (code == -6){
            return "定时发送时间不是有效的时间格式";
        }
        else if (code == -7){
            return "提交信息末尾未加签名，请添加中文的企业签名【 】;内容是乱码，请针对内容转码成GB2312或GBK\n";
        }
        else if (code == -8){
            return "发送内容需在1到300字之间";
        }
        else if (code == -9){
            return "发送号码为空";
        }
        else if (code == -10){
            return "定时时间不能小于系统当前时间";
        }
        else if (code == -100){
            return "IP黑名单";
        }
        else if (code == -102){
            return "账号黑名单";
        }
        else if (code == -103){
            return "IP未导白";
        }
        else{
            return "其他未知错误, 返回值：" + code;
        }
    }
}
